"use strict";
// 商家 
const Controller = require("egg").Controller;
class ShopController extends Controller {
  // 创建 编辑 商家信息
  async edit() {
    const { ctx, app } = this;
    const { id, title, logo, banner, lat, log, class_id, city_id, popularity, equipment, address, address_detaill, business, phone, detail, detail_images, sort, status, is_recommend } = ctx.request.body
    ctx.validate({
      title: {
        type: "string",
        required: true,
        desc: "商家名称",
      },
      logo: {
        type: "string",
        required: true,
        desc: "商家封面图",
      },
      banner: {
        type: "string",
        required: true,
        desc: "商家轮播图",
      },
      lat: {
        type: "string",
        required: true,
        desc: "经度",
      },
      log: {
        type: "string",
        required: true,
        desc: "纬度",
      },
      class_id: {
        type: "int",
        required: true,
        desc: "分类id",
      },
      city_id: {
        type: "int",
        required: true,
        desc: "城市id",
      },
      popularity: {
        type: "int",
        required: true,
        desc: "人气",
      },
      equipment: {
        type: "string",
        required: true,
        desc: "设备",
      },
      address: {
        type: "string",
        required: true,
        desc: "地址",
      },
      address_detaill: {
        type: "string",
        required: true,
        desc: "详细地址",
      },
      business: {
        type: "string",
        required: true,
        desc: "营业时间",
      },
      phone: {
        type: "int",
        required: true,
        desc: "联系电话",
      },
      detail: {
        type: "string",
        required: true,
        desc: "商家简介",
      },
      detail_images: {
        type: "string",
        required: true,
        desc: "商家图片",
      },
      sort: {
        type: "int",
        required: true,
        desc: "排序",
      },
      status: {
        type: "int",
        required: true,
        range: {
          in: [0, 1]
        },
        desc: "状态",
      }
    });
    //判断 分类  城市 是否存在
    if (!(await app.model.Class.findByPk(class_id))) {
      ctx.throw(400, '分类不存在')
    }
    if (!(await app.model.City.findByPk(class_id))) {
      ctx.throw(400, '城市不存在')
    }
    let listOne, listTwo, listThree = {}, transaction
    if (id == 0) {//创建
      listTwo = await app.model.Shop.findOne({
        where: {
          title
        }
      })
      if (listTwo) {
        ctx.throw(400, '商家名称已存在')
      }
      try {
        // 开启事务
        transaction = await app.model.transaction();
        // 创建 商家信息
        listOne = await app.model.Shop.create({
          title, logo, banner, lat, log, class_id, city_id, popularity, equipment, address, address_detaill, business, phone, detail, detail_images, is_recommend, sort, status
        }, { transaction })
        // 创建 登录信息
        listThree = await app.model.ShopUser.create({
          shop_id: listOne.id,
          nickname: title,
          phone,
          pwd: phone.substr(5, 6),
          role: 'shop_admin',
          status: '1'
        }, { transaction })
        //创建 配置表 总金额 可提现金额 客户数量
        await app.model.ShopConfig.creeat({
          shop_id: listOne.id, customer_total: 0, withd_amount: 0, total_amount:0
        })
        // console.log('***********', listOne)
        // 提交事务
        await transaction.commit();
      } catch (e) {
        // 事务回滚
        await transaction.rollback();
        return ctx.apiFail('事务，操作失败');
      }
    } else { //修改
      listTwo = await app.model.Shop.findOne({
        where: {
          id: id
        }
      })
      if (!listTwo) {
        return ctx.apiFail('数据不存在')
      }
      listOne = await listTwo.update({
        title, logo, banner, lat, log, class_id, city_id, popularity, equipment, address, address_detaill, business, phone, detail, detail_images, is_recommend, sort, status
      })
    }
    ctx.apiSuccess(listOne);
  }

  // 查询 商家列表
  async list() {
    const { ctx, app } = this
    const { city_id, class_id, status, title } = ctx.query
    let Op = app.Sequelize.Op;
    let where = {}
    if (city_id != '' && city_id > 0) {
      where.city_id = city_id
    }
    if (class_id != '' && class_id > 0) {
      where.class_id = class_id
    }
    if (status != '' && status != undefined) {
      where.status = status == 0 ? '0' : '1'

    }
    // console.log('******',where)
    // return
    if (title != '' && title != undefined) {
      where.title = {
        [Op.like]: `%${title}%`

      }
    }
    let list = await ctx.page(app.model.Shop, where)
    ctx.apiSuccess(list)

  }
  // 删除
  async del() {
    const { ctx, app } = this
    const id = ctx.params.id
    const list = await app.model.Shop.findByPk(id)
    if (!list) {
      ctx.throw(400, '数据不存在')
    }
    let transaction
    try {
      // 开启事务
      transaction = await app.model.transaction();
      await app.model.ShopUser.destroy({
        where: {
          shop_id: list.id
        }
      }, { transaction });
      await list.destroy({}, { transaction })
    } catch (e) {
      // 事务回滚
      await transaction.rollback();
      return ctx.apiFail('事务，操作失败');
    }
    ctx.apiSuccess('删除成功')
  }
}
module.exports = ShopController;
